<template>
  <el-form ref="edit" :model="data" :rules="rules" label-width="70px">
    <el-form-item label="账号" prop="username">
      <el-input v-model="data.username" />
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="data.name" />
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="data.phone" />
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio-group v-model="data.sex" class="ml-4">
        <el-radio label="0" size="large">男</el-radio>
        <el-radio label="1" size="large">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="身份证" prop="idNumber">
      <el-input v-model="data.idNumber" />
    </el-form-item>
    <el-form-item>
      <span class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="updateEmp"> 确认 </el-button>
      </span>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { getEmpDetailApi, updateEmpApi } from '@/api/index.js'
import { ref, defineEmits, watch, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'

const emits = defineEmits(['closeEditDia'])
//添加用户需要数据
const data = ref({
  username: '',
  name: '',
  phone: '',
  sex: '1',
  idNumber: ''
})
const rules = {
  username: [{ required: true, message: '账号必填', trigger: 'blur' }],
  name: [{ required: true, message: '姓名必填', trigger: 'blur' }],
  phone: [{ required: true, message: '手机号必填', trigger: 'blur' }],
  idNumber: [{ required: true, message: '身份证必填', trigger: 'blur' }]
}
const edit = ref()
//获取用户信息
const getEmpDetail = async(id) => {
  const res = await getEmpDetailApi(id)
  data.value = res.data
}
//修改用户信息
const updateEmp = () => {
  edit.value.validate(async (isOk) => {
    if (!isOk) return
    try {
      const res = await updateEmpApi(data.value)
      if (res.code === 200) {
        ElMessage.success('修改成功')
        emits('closeEditDia')
      } else {
        ElMessage.error(res.msg)
      }
    } catch (err) {
      console.log(err)
    }
    add.value.resetFields()
  })
}

const close = () => {
  add.value.resetFields()
  emits('closeEditDia')
}
defineExpose({
  getEmpDetail
})
</script>
